package com.hskn.hss.app.cases.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.cases.entity.Cases;
import com.hskn.hss.module.cases.mapper.CasesMapper;
import com.hskn.hss.module.cases.service.ICasesService;
import com.hskn.hss.utils.AttributeUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * APP案件数据
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Api(tags = {"-case"}, description = "案件数据")
@Slf4j
@RestController
@RequestMapping("/hss/app/case")
public class APPCasesController {

    @Resource
    private CasesMapper casesMapper;
    @Autowired
    private ICasesService casesService;

    /**
     * 首页案件数据
     * @param
     * @return
     */
    @ApiOperation(value = "首页案件数据", notes = "首页案件数据")
    @RequestMapping(value = "selectCases", method = RequestMethod.POST)
    public AjaxResult<Object> selectAlarm() {
        try {
            String day = DateUtil.formatDate(new Date());
            String startTime = AttributeUtils.timeCompletion(day, 's');
            String endTime = AttributeUtils.timeCompletion(day, 'e');

            QueryWrapper<Cases> casesQueryWrapper = new QueryWrapper<>();
            casesQueryWrapper.lambda().ge(Cases::getCreateTime,startTime);
            casesQueryWrapper.lambda().le(Cases::getCreateTime,endTime);
            casesQueryWrapper.lambda().orderByDesc(Cases::getCreateTime);
            List<Cases> casess = casesMapper.selectList(casesQueryWrapper);

            //逾期的案件
            List<Cases> overdueList = new ArrayList<>();
            if (Objects.nonNull(casess)){
                overdueList = casess.stream().filter(cases -> 1 == cases.getIsOverdue()).collect(Collectors.toList());
            }
            //未结案的紧急案件
            List<Cases> urgentList = new ArrayList<>();
            if (Objects.nonNull(casess)){
                urgentList = casess.stream().filter(cases -> (1 == cases.getStatus() || 2 == cases.getStatus() || 3 == cases.getStatus()) && "3".equals(cases.getCaseLevelId())).collect(Collectors.toList());
            }

            Map<String,Object> casesMap = new HashMap();
            casesMap.put("reportToday",casess.size());
            casesMap.put("overdueCases",overdueList.size());
            casesMap.put("urgentCase",urgentList.size());
            casesMap.put("casesList",casess);

            return AjaxResult.success(casesMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> applist(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = casesService.appmList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}
